// -----------------------------
// Author: 		Dhaenens Gert	
// Creation: 	1/6/2015 12:56:37 PM				
// -----------------------------

#include "ReverbPass.h"

#include "LowPass.h"
#include "HighPass.h"
#include "DelayPass.h"

ReverbPass::ReverbPass()
{
}

ReverbPass::~ReverbPass()
{
}

SoundSample ReverbPass::Apply(const SoundSample& input) const
{
	// Copy the Sample
	SoundSample sample = input;

	// Apply HighPass Filter
	HighPass highPass;
	highPass.CutoffFrequency = LowCut;
	highPass.Gain = 0.7f;
	highPass.QualityFactor = 1.0f / Math::Sqrt(2.0f);
	highPass.Modulate = false;
	sample = highPass.Apply(sample);
	
	// Apply Low Pass Filter
	LowPass lowPass;
	lowPass.CutoffFrequency = HighCut;
	lowPass.Gain = 1.0f;
	lowPass.QualityFactor = 1.0f / Math::Sqrt(2.0f);
	lowPass.Modulate = false;
	sample = lowPass.Apply(sample);

	// Create the Initial Delay Sample
	DelayPass preDelayPass;
	preDelayPass.DelayOffset = PreDelay;
	preDelayPass.UseDecay = true;
	preDelayPass.Decay = Decay;
	preDelayPass.DecayStart = 1.0f;
	sample = preDelayPass.Apply(sample);

	// Apply more delays
	UInt32 delayCount = 8;
	for (UInt32 i = 0; i < delayCount; ++i)
	{
		DelayPass delayPass;
		delayPass.DelayOffset = PreDelay + (Math::Pow(2.0f,(Int32) i) * 0.001f);
		delayPass.UseDecay = true;
		delayPass.Decay = Decay;
		delayPass.DecayStart = 1.0f - (Math::Pow(2.0f, (Int32)i) * 0.001f);
		sample = delayPass.Apply(sample);
	}

	// Apply the Low Pass Again
	// To filter out the metal tones
	sample = lowPass.Apply(sample);

	return sample;
}